package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.accident;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.InputStream;
import java.util.Arrays;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
 * description:
 * 意外险风险保额策略加载
 *
 * @author jackdaw
 * @date 2020-03-30 15:48
 */
public class AccidentStrategyFactory {
    private static Logger logger = LoggerFactory.getLogger(AccidentStrategyFactory.class);
    private static ConcurrentHashMap<String, AccidentStrategy> accidentStrategyMap = new ConcurrentHashMap<>();

    public static AccidentStrategy getStrategy(String riskCode) {
        return accidentStrategyMap.get(riskCode);
    }

    static {
        try {
            Resource resource = new ClassPathResource("accidentstrategy.properties");
            InputStream inputStream = resource.getInputStream();
            Properties properties = new Properties();
            properties.load(inputStream);
            for (String key : properties.stringPropertyNames()) {
                AccidentStrategy strategy = (AccidentStrategy) Class.forName(properties.getProperty(key)).newInstance();
                Arrays.stream(key.split(",")).forEach(s -> {
                    accidentStrategyMap.put(s, strategy);
                    logger.info("accidentStrategyMap add key: " + s + ", new instance:" + strategy);
                });
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

}
